﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：贺兴祥
// Email: hxx1203@hotmail.com
// 日 期：2011/11/02
// 描 述：树形列表菜单
// 版 本：2.00
// 
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// V2.01  2011/11/02   贺兴祥            功能菜单选项      
//    
//==================================================================

using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using ZX.DBModule;
using ZX.BLL.Extend;

namespace ZX.Web.UI.Controls
{
    public class TreeViewMenu : Literal
    {
        private string _tablename = string.Empty;
        [Description("数据来源的表名")]
        public string TableName
        {
            get { return this._tablename; }
            set { _tablename = value; }
        }
        private string _caption = "";
        [Description("显示文本")]
        public string Caption
        {
            get { return this._caption; }
            set { _caption = value; }
        }
        private string _key = "";
        [Description("主键")]
        public string Key
        {
            get { return this._key; }
            set { _key = value; }
        }
        private bool _isAllowCheck = false;
        [Description("是否允许多选")]
        public bool AllowCheck
        {
            get { return this._isAllowCheck; }
            set { _isAllowCheck = value; }
        }
        private int _isCustom = 0;
        [Description("自定义类型(0:无条件类型 1:字典表类型)")]
        public int IsCustom
        {
            get { return this._isCustom; }
            set { _isCustom = value; }
        }
        private string _isCustomValue="";
        [Description("自定义条件值")]
        public string IsCustomValue
        {
            get { return this._isCustomValue; }
            set { _isCustomValue = value; }
        }
        private string _isCustomText = "";
        [Description("自定义文本值")]
        public string IsCustomText
        {
            get { return this._isCustomText; }
            set { _isCustomText = value; }
        }

        //<div id='Div1' class='whitebg'>
        //    <ul>
        //        <li value='09937d9e-385a-4d1d-8b53-ecb0ddbe83a0'>
        //            <span><a style='display: block; cursor: hand' onclick="Native('工程部','09937d9e-385a-4d1d-8b53-ecb0ddbe83a0')">工程部</a>
        //            </span></li>
        //    </ul>
        //</div>
        /// <summary>
        /// 生成菜单列表
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                string sql = "", strReturn = "";
                if (IsCustom == 0) //无条件
                {
                    sql = "select " + Caption + " as Caption," + Key + " as [key] from " + TableName + " order by " + Caption + "";
                }
                else if (IsCustom == 1)   //自定义条件查询字典表数据
                {
                    sql = "select " + Caption + " as Caption," + Key + " as [key] from " + TableName + " where ClassID=(select ClassID from DatPhraseClass where ClassValue='" + IsCustomValue + "') order by " + Caption + "";
                }
                else if (IsCustom == 2)
                {
                    sql = "select " + Caption + " as Caption," + Key + " as [key] from " + TableName + " where " + IsCustomValue + " order by " + Caption;
                }
                IList<TreeViewData> list = new EnttityBLL().GetEntitysBySqlComm<TreeViewData>(sql);

                if (IsCustom == 0 || IsCustom == 2)
                {
                    strReturn = LoadData0(list);
                }
                else if (IsCustom == 1)
                {
                    strReturn = LoadData1(list);
                }
                writer.Write(strReturn);
                base.Render(writer);
            }
            else
            {
                writer.Write("树型列表菜单");
            }
        }

        private string LoadData0(IList<TreeViewData> list)
        {
            StringBuilder _strHtml = new StringBuilder();
            if (AllowCheck)
            {
                _strHtml.Append("<div id='" + this.ID + "' class='whitebg ygtv-checkbox'>");
            }
            else
            {
                _strHtml.Append("<div id='" + this.ID + "' class='whitebg'>");
            }
            _strHtml.Append("<ul>");
            foreach (TreeViewData tree in list)
            {
                _strHtml.Append("<li>");
                if (!AllowCheck)
                {
                    _strHtml.Append("<span><a href=\"#" + tree.Key + "\" style='display:block;cursor:hand' onclick=\"Native('" + tree.Caption + "','" + tree.Key + "')\">" + tree.Caption + "</a></span>");
                }
                else
                {
                    _strHtml.Append("<a href='#@" + tree.Key + "'>" + tree.Caption + "</a>");
                }
                _strHtml.Append("</li>");
            }
            _strHtml.Append("</ul>");
            _strHtml.Append("</div>");
            return _strHtml.ToString();
        }
        /// <summary>
        /// 自定义时加载树形数据(字典数据)
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private string LoadData1(IList<TreeViewData> list) 
        {
            StringBuilder _strHtml = new StringBuilder();
            if (AllowCheck)
            {
                _strHtml.Append("<div id='" + this.ID + "' class='whitebg ygtv-checkbox'>");
            }
            else
            {
                _strHtml.Append("<div id='" + this.ID + "' class='whitebg'>");
            }
            _strHtml.Append("<ul><li>");
            _strHtml.Append("<span><a href=\"#DutyInfoType\" style=\"display: block; cursor: hand\" onclick=\"Native('" + IsCustomText + "','DutyInfoType','1')\">" + IsCustomText + "</a></span>");
            _strHtml.Append("<ul>");
            foreach (TreeViewData tree in list)
            {
                _strHtml.Append("<li>");
                if (!AllowCheck)
                {
                    _strHtml.Append("<span><a href=\"#" + tree.Key + "\" style='display:block;cursor:hand' onclick=\"Native('" + tree.Caption + "','" + tree.Key + "')\">" + tree.Caption + "</a></span>");
                }
                else
                {
                    _strHtml.Append("<a href='#@" + tree.Key + "'>" + tree.Caption + "</a>");
                }
                _strHtml.Append("</li>");
            }
            _strHtml.Append("</ul></li></ul>");
            _strHtml.Append("</div>");
            return  _strHtml.ToString();
        }
    }
}
